package com.lagou.edu.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.lagou.edu.dao.CodeDao;
import com.lagou.edu.pojo.LagouAuthCode;
import com.lagou.edu.util.MailUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;


import java.util.Date;

@Service
public class EmailServiceImpl implements EmailService {


    @Autowired
    private CodeDao codeDao;
    @Override
    public Boolean createEmail(String email, String code) {
        MailUtil mailUtil = new MailUtil();
        try {
            mailUtil.sendMail(email,code);
        }  catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    @Override
    public Integer validateEmail(String email, String code) {
        LagouAuthCode lagouAuthCode = new LagouAuthCode();
        lagouAuthCode.setEmail(email);
        lagouAuthCode.setCode(code);
        Example<LagouAuthCode> example = Example.of(lagouAuthCode);
        lagouAuthCode = codeDao.findOne(example).get();
        if(lagouAuthCode==null){
            return 1;
        }else if(lagouAuthCode.getExpiretime().before(new Date())){
            return 2;
        }else{
            return 0;
        }
    }
}
